package com.zuoqi.easywork.system.controller;

import com.github.pagehelper.Page;
import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.PageView;
import com.zuoqi.easywork.system.service.EasyworkService;
import com.zuoqi.easywork.userManage.mapper.TcUserMapper;
import com.zuoqi.easywork.userManage.model.TcUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller

public class LoginController {
    @Autowired
    private TcUserMapper tcUserMapper;

    @Autowired
    private EasyworkService easyworkService;

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);


    /*登入页
    * */
    @RequestMapping(value = "/login")
    public String testLogin ( ModelMap modelMap){
        return "/login";
    }

    /*端口号跳转
    * */
    @RequestMapping("/")
    public String home() {
        log.info("enter home..");
        return "redirect:/login";
    }

    /**
     * 登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/ajax/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request,HttpSession session) {
        IMap param = new IMap(request);
        String username = param.getString("username");
        String password = param.getString("password");
        if (StringUtils.isBlank(username)) {
            request.setAttribute("errorMessage", "用户名不能为空");
            return "/login";
        }
        if (StringUtils.isBlank(password)) {
            request.setAttribute("errorMessage", "密码不能为空");
            return "/login";
        }
        try {
            Boolean checkLogin = easyworkService.checkLogin(param);
            if(!checkLogin){
                request.setAttribute("errorMessage", "用户名密码错误");
                return "login";
            }else {
                TcUser tcUser = easyworkService.getLoginUser(param);
                session.setAttribute("loginUserId",tcUser.getUserId());
                session.setAttribute("loginUserName",tcUser.getUserName());
                session.setAttribute("loginUserRole",tcUser.getRoleId());
                session.setAttribute("loginUserDepart",tcUser.getDepartId());
                log.debug(tcUser.getUserId().toString());
                return "redirect:/index";
            }
        }catch (Exception e) {
            request.setAttribute("errorMessage", "用户名或者密码不正确");
            return "/login";
        }
    }

    /**
     * 登出
     * @param
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(){
        return "redirect:/";
    }

    @RequestMapping(value = "/index")
    public String indexView (ModelMap modelMap){
        return "/index";
    }
}
